<?php
namespace App\Libs\MarketplaceWebServiceOrders;
use App\Libs\MarketplaceWebServiceOrders\Model\GetOrderRequest;
use App\Libs\MarketplaceWebServiceOrders\Model\ListOrderItemsByNextTokenRequest;
use App\Libs\MarketplaceWebServiceOrders\Model\ListOrderItemsRequest;
use App\Libs\MarketplaceWebServiceOrders\Model\ListOrdersByNextTokenRequest;
use App\Libs\MarketplaceWebServiceOrders\Model\ListOrdersRequest;
class OrdersMws{
    private static $_instance = null;

    private $_fields = ['SellerId',
                        'MWSAuthToken',
                        'CreatedAfter',
                        'CreatedBefore',
                        'LastUpdatedAfter',
                        'LastUpdatedBefore',
                        'OrderStatus',
                        'MarketplaceId',
                        'FulfillmentChannel',
                        'PaymentMethod',
                        'BuyerEmail',
                        'SellerOrderId',
                        'MaxResultsPerPage',
                        'TFMShipmentStatus',
                        'AmazonOrderId',
                        'NextToken'];

    public static function getInit(){
        if(!is_object(self::$_instance)){
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    /**
     * 根据您指定的 AmazonOrderId 值返回订单，最多可返回 50 个
     * API GetOrder
     * @param array $con
     * @return array
     */
    public function getOrder($con = array())
    {
        $request = $this->_getRequest(new GetOrderRequest(),$con);
        if (!($request instanceof GetOrderRequest)) {
            $request = new GetOrderRequest($request);
        }
        $parameters = $request->toQueryParameterArray();
        $parameters['Action'] = 'GetOrder';
        return $parameters;
    }

    /**
     * 返回您在指定时间段内所创建或更新的订单。
     * 可以通过 CreatedAfter 参数或 LastUpdatedAfter 参数来指定时间段。您必须使用其中一个参数，但不可同时使用两个参数。
     * API ListOrders
     * @param array $con
     * @return array
     */
    public function listOrders($con = array())
    {
        $request = $this->_getRequest(new ListOrdersRequest(),$con);
        if (!($request instanceof ListOrdersRequest)) {
            $request = new ListOrdersRequest($request);
        }
        $parameters = $request->toQueryParameterArray();
        $parameters['Action'] = 'ListOrders';
        return $parameters;
    }

    /**
     * 使用 NextToken 参数返回下一页订单。
     * API ListOrdersByNextToken
     * @param array $con
     * @return array
     */
    public function listOrdersByNextToken($con = array())
    {
        $request = $this->_getRequest(new ListOrdersByNextTokenRequest(),$con);
        if (!$request instanceof ListOrdersByNextTokenRequest) {
            $request = new ListOrdersByNextTokenRequest($request);
        }
        $parameters = $request->toQueryParameterArray();
        $parameters['Action'] = 'ListOrdersByNextToken';
        return $parameters;
    }

    /**
     * 根据指定的 AmazonOrderId 返回订单商品。
     * API ListOrderItems
     * @param array $con
     * @return array
     */
    public function listOrderItems($con = array())
    {
        $request = $this->_getRequest(new ListOrderItemsRequest(),$con);
        if (!($request instanceof ListOrderItemsRequest)) {
            $request = new ListOrderItemsRequest($request);
        }
        $parameters = $request->toQueryParameterArray();
        $parameters['Action'] = 'ListOrderItems';
        return $parameters;
    }

    /**
     * 使用 NextToken 参数返回下一页订单商品。
     * API ListOrderItemsByNextToken
     * @param array $con
     * @return array
     */
    public function listOrderItemsByNextToken($con = array())
    {
        $request = $this->_getRequest(new ListOrderItemsByNextTokenRequest(),$con);
        if (!$request instanceof ListOrderItemsByNextTokenRequest) {
            $request = new ListOrderItemsByNextTokenRequest($request);
        }
        $parameters = $request->toQueryParameterArray();
        $parameters['Action'] = 'ListOrderItemsByNextToken';
        return $parameters;
    }

    private function _getRequest($request,$con = array()) {
        foreach($con as $k=>$v){
            if($k&&in_array($k,$this->_fields)){
                $setter = "set$k";
                $request->$setter($v);
            }
        }
        return $request;
    }
}